package com.living.player;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TableLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import tv.danmaku.ijk.media.player.IjkMediaPlayer;

import com.living.R;
import com.living.player.event.EventStatusPause;
import com.living.player.event.EventStatusPlay;
import com.living.player.widget.PHAudioView;
import com.living.player.widget.media.CustomMediaController;
import com.living.player.widget.media.IjkVideoView;
import com.living.player.widget.media.OnPauseResumeListener;
import com.md.basedpc.common.PHThreadPool;
import com.md.livingwidget.uiutils.SvgUtil;

import org.greenrobot.eventbus.EventBus;

public class LivePlayerView extends LivePlayerBase {

    public IjkVideoView mVideoView;
    private ImageView mOrderButton;
    private ImageView mMenuButton;
    CustomMediaController costomMediaController;

    public CustomMediaController getCostomMediaController() {
        return costomMediaController;
    }

    public LivePlayerView(@NonNull Context context) {
        super(context);
    }

    public LivePlayerView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public LivePlayerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    protected void init() {
        LayoutInflater.from(getContext()).inflate(R.layout.ph_player_audio_layout, this);
        TableLayout mHudView = findViewById(R.id.mHudView);
        mVideoView = findViewById(R.id.video_view);
        mVideoView.setHudView(mHudView);
        costomMediaController = findViewById(R.id.costomMediaController);
        mVideoView.setMediaController(costomMediaController);
        mOrderButton = costomMediaController.getmOrderButton();
        mMenuButton = costomMediaController.getmMenuButton();
        mVideoView.setOnIjkPlayStateListener(this);

        mOrderButton.setImageDrawable(SvgUtil.convertSvg2SkinDrawable(getContext(), R.drawable.icon_play_control_order_all, Color.parseColor("#bfbfbf")));
    }

    public void setPrevNextListeners(View.OnClickListener next, View.OnClickListener prev) {
        costomMediaController.setPrevNextListeners(next, prev);
    }

    public void setOnPauseResumeListener(OnPauseResumeListener onPauseResumeListener) {
        costomMediaController.setOnPauseListeners(onPauseResumeListener);
    }

    public ImageView getOrderButton() {
        return mOrderButton;
    }

    public ImageView getMenuButton() {
        return mMenuButton;
    }

    /***
     * 播放
     */
    public void playMadia(String mVideoPath) {
        if (TextUtils.equals(this.mVideoPath,mVideoPath)) {
            return;
        }
        this.mVideoPath = mVideoPath;
        mVideoView.stopPlayback();
        // prefer mVideoPath
        if (mVideoPath != null) {
            mVideoView.setVideoPath(mVideoPath);
            mVideoView.start();
        }
    }


    public void enterBackground() {
        mVideoView.enterBackground();
        IjkMediaPlayer.native_profileEnd();
    }

    @Override
    public void onPrepare() {
        super.onPrepare();
        PHThreadPool.getInstance().executeUIThread(new Runnable() {
            @Override
            public void run() {
                costomMediaController.setEnabled(true);
            }
        });

    }

    @Override
    public void onStart() {
        super.onStart();
        PHThreadPool.getInstance().executeUIThread(new Runnable() {
            @Override
            public void run() {
                costomMediaController.updatePausePlay();
                EventBus.getDefault().post(new EventStatusPlay());
            }
        }, 500);
    }

    @Override
    public void onPause() {
        super.onPause();
        PHThreadPool.getInstance().executeUIThread(new Runnable() {
            @Override
            public void run() {
                costomMediaController.updatePausePlay();
                EventBus.getDefault().post(new EventStatusPause());
            }
        });
    }

    @Override
    public void onError() {
        super.onError();
        PHThreadPool.getInstance().executeUIThread(new Runnable() {
            @Override
            public void run() {
                costomMediaController.updatePausePlay();
                EventBus.getDefault().post(new EventStatusPause());
            }
        });
    }

    @Override
    public void onComplete() {
        super.onComplete();
        PHThreadPool.getInstance().executeUIThread(new Runnable() {
            @Override
            public void run() {
                costomMediaController.updatePausePlay();
                costomMediaController.setEnabled(false);
                EventBus.getDefault().post(new EventStatusPause());
                mVideoView.attachPlay();
            }
        });
    }

}
